package com.gxgy.app.controller;

import com.gxgy.common.core.domain.R;
import com.gxgy.common.exception.ServiceException;
import com.gxgy.common.utils.StringUtils;
import com.gxgy.app.doman.po.User;
import com.gxgy.app.doman.vo.UserVo;
import com.gxgy.app.service.UserService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@RequestMapping("/app/login")
public class LoginController {

    @Resource
    private UserService userService;


    @PostMapping("/login")
    public R<UserVo> login(@RequestBody User user){
        // 判断前端传递的是否是空
        if (StringUtils.isNull(user)) {
            throw new ServiceException("用户信息为空");
        }
        UserVo login = userService.login(user);
        return R.ok(login);
    }

    @PostMapping("/register")
    public R<UserVo> register(@RequestBody User user) {
        // 判断前端传递的是否是空
        if (StringUtils.isNull(user)) {
            throw new ServiceException("用户信息为空");
        }
        // 调用服务层注册
        boolean registerOk = userService.registerUser(user);
        if (!registerOk) {
            return R.fail("注册失败");
        }
        return R.ok();
    }
}
